<?php

namespace app\company\controller\report\subset;

use app\BaseController;
use app\model\RatingScaleModel;
use app\model\TaskModel;
use app\model\TaskUserResultModel;
use app\model\UnitModel;
use think\App;

class TaskReport extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }


    /**
     * 任务测评报告
     * @return false|string
     */
    public function Index(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'unit_id|单位ID' => 'require'
            ]);
            $TaskModel = new TaskModel();
            $where = [];
            $whereOr = [];
            if (!empty($param['name'])) {
                $task_id = $TaskModel->where([
                    ['name', 'like', '%' . $param['name'] . '%'],
                    ['unit_id','=',$param['unit_id']]
                ])->column('id');
                $whereOr[] = ['task_id', 'in', $task_id];
                $RatingScaleModel = new RatingScaleModel();
                $RatingScaleIds = $RatingScaleModel->where([
                    ['name', 'like', '%' . $param['name'] . '%'],
                    ['unit_id','=',$param['unit_id']]
                ])->column('id');
                $whereOr[] = ['rating_scale_id', 'in', $RatingScaleIds];
            }
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where[] = ['start_time', 'between', [$param['start_time'], $param['end_time']]];
            } else {
                if (!empty($param['start_time'])) {
                    $where[] = ['start_time', '>=', $param['start_time']];
                }
                if (!empty($param['end_time'])) {
                    $where[] = ['start_time', '<=', $param['end_time']];
                }
            }
            if (!empty($param['start_time2']) && !empty($param['end_time2'])) {
                $where[] = ['time', 'between', [$param['start_time2'], $param['end_time2']]];
            } else {
                if (!empty($param['start_time2'])) {
                    $where[] = ['time', '>=', $param['start_time2']];
                }
                if (!empty($param['end_time2'])) {
                    $where[] = ['time', '<=', $param['end_time2']];
                }
            }
            if (!empty($param['uid'])) {
                $where[] = ['uid', '=', $param['uid']];
            }
            if (isset($param['is_warning']) && strlen($param['is_warning']) > 0) {
                if ($param['is_warning'] == 1) {
                    $where[] = ['warning_num', '>', 0];
                } else {
                    $where[] = ['warning_num', '=', 0];
                }
            }
            $TaskUserResultModel = new TaskUserResultModel();
            $TaskIds = $TaskUserResultModel->where('unit_id', '=', $param['unit_id'])->whereOr($whereOr)->column('task_id');
            if (count($TaskIds) > 0) {
                $where[] = ['id', 'in', $TaskIds];
            } else {
                $where[] = ['id', '=', -1];
            }
//            $where[] = ['unit_id', '=', $param['unit_id']];
            $query = $TaskModel->where($where);
            $data = $query->field('id,uid,name,rating_scale_count,user_count,start_time,end_time,user_complete_num,result_read_auth,warning_num,user_complete_rate,time')
                ->with(['userName'])
                ->withCount(['AnswerRoleUser', 'AnswerRoleParent'])
                ->order('id desc')->paginate($param['pageSize'])->toArray();
            foreach ($data['data'] as &$item){
                $item['answer_role'] = $TaskUserResultModel->where([
                    ['task_id','=',$item['id']],
                    ['answer_role','=',1]
                ])->count();
                $item['answer_role2'] = $TaskUserResultModel->where([
                    ['task_id','=',$item['id']],
                    ['answer_role','=',2]
                ])->count();
            }
            $data['ids'] = $query->column('id');
            $UnitModel = new UnitModel();
            $data['unit_name'] = $UnitModel->where('id','=',$param['unit_id'])->cache(true,60)->value('name');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

}